package com.book.pay.strategy;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.book.pojo.Order;
import com.book.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @param
 * @author
 * @version 1.0.4
 * @description:TODO
 * @date 2024/1/2 22:11
 */
@Service
public class AlipayStrategy  implements PayStrateInterface {


    @Override
    public String pay(Order order) {
        // 具体支付逻辑
        AlipayClient client = new DefaultAlipayClient(Constants.ALIPAY_GATEWAY, Constants.APP_ID,
                Constants.APP_PRIVATE_KEY, "JSON", "UTF-8",
                Constants.ALIPAY_PUBLIC_KEY, Constants.SIGN_TYPE);
        // 设置请求参数
        AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();

        payRequest.setBizContent("{\"out_trade_no\":\"" + order.getOrderId() + "\","
                + "\"total_amount\":\"" +  order.getPrice() + "\","
                + "\"subject\":\"欧阳钊\","
                + "\"body\":\"商品名称\","
                + "\"timeout_express\":\"90m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        try {
            AlipayTradePagePayResponse response = client.pageExecute(payRequest, "GET");
            return response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException("Alipay faild" + e);
        }

    }


}
